package com.szgr.baoyin.pay;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MainActivity;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.AptitudeSubmitActivity;
import com.szgr.baoyin.activity.BankTranslateActivity;
import com.szgr.baoyin.activity.BaseActivity;
import com.szgr.baoyin.activity.DuYinCardPayActivity;
import com.szgr.baoyin.activity.FinanceProcessActivity;
import com.szgr.baoyin.activity.WebActivity;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class AccessoryProtectPayActivity extends BaseActivity {

    private CheckBox cb_01;
    private CheckBox cb_02;
    private CheckBox cb_03;
    private CheckBox cb_04;
    private CheckBox cb_05;
    private CheckBox cb_06;
    private String pay_sn;
    private int position = -1;
    private String type = "";
    private View pay_01;
    private View pay_02;
    private View pay_03;
    private View pay_04;
    private View pay_05;
    private View pay_06;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_accessory_protect_pay);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("支付");
    }

    @Override
    public void initView() {
        pay_01 = findViewById(R.id.pay_01);
        pay_01.setOnClickListener(this);//都银卡
        pay_02 = findViewById(R.id.pay_02);
        pay_02.setOnClickListener(this);//支付宝
        pay_03 = findViewById(R.id.pay_03);
        pay_03.setOnClickListener(this);//企业银联
        pay_04 = findViewById(R.id.pay_04);
        pay_04.setOnClickListener(this);//银联
        pay_05 = findViewById(R.id.pay_05);
        pay_05.setOnClickListener(this);//融资
        pay_06 = findViewById(R.id.pay_06);
        pay_06.setOnClickListener(this);//银行转账
        cb_01 = (CheckBox) findViewById(R.id.pay_box_01);
        cb_02 = (CheckBox) findViewById(R.id.pay_box_02);
        cb_03 = (CheckBox) findViewById(R.id.pay_box_03);
        cb_04 = (CheckBox) findViewById(R.id.pay_box_04);
        cb_05 = (CheckBox) findViewById(R.id.pay_box_05);
        cb_06 = (CheckBox) findViewById(R.id.pay_box_06);
        cb_01.setOnClickListener(this);
        cb_02.setOnClickListener(this);
        cb_03.setOnClickListener(this);
        cb_04.setOnClickListener(this);
        cb_05.setOnClickListener(this);
        cb_06.setOnClickListener(this);

        findViewById(R.id.pay_confirm).setOnClickListener(this);
        findViewById(R.id.go_to_rule).setOnClickListener(this);

        Intent intent = getIntent();
        if(intent != null){
            if (intent.hasExtra("pay_sn")) {
                pay_sn = intent.getStringExtra("pay_sn");
            }
            if(intent.hasExtra("type")){
                type = intent.getStringExtra("type");
                switch (type){
                    case Constant.TYPE_01:
                        pay_01.setVisibility(View.GONE);
                        break;
                    case Constant.TYPE_02:
                        //TODO 都银卡不能用卡支付，不能租赁
                        pay_01.setVisibility(View.GONE);
                        pay_05.setVisibility(View.GONE);
                        break;
                    case Constant.TYPE_03:
                        //TODO 配件。维保不能租赁
                        pay_05.setVisibility(View.GONE);
                        break;
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.pay_01:
            case R.id.pay_box_01:
                changeBoxState(cb_01, cb_02, cb_03, cb_04, cb_05, cb_06);
                position = 0;
                break;
            case R.id.pay_02:
            case R.id.pay_box_02:
                changeBoxState(cb_02, cb_01, cb_03, cb_04, cb_05, cb_06);
                position = 1;
                break;
            case R.id.pay_03:
            case R.id.pay_box_03:
                changeBoxState(cb_03, cb_02, cb_01, cb_04, cb_05, cb_06);
                position = 2;
                break;
            case R.id.pay_04:
            case R.id.pay_box_04:
                changeBoxState(cb_04, cb_02, cb_03, cb_01, cb_05, cb_06);
                position = 3;
                break;
            case R.id.pay_05:
            case R.id.pay_box_05:
                changeBoxState(cb_05, cb_02, cb_03, cb_04, cb_01, cb_06);
                position = 4;
                break;
            case R.id.pay_06:
            case R.id.pay_box_06:
                changeBoxState(cb_06, cb_02, cb_03, cb_04, cb_05, cb_01);
                position = 5;
                break;
            case R.id.pay_confirm:
                if(position != -1){
                    switch (position){
                        case 0://都银卡抵扣
                            goDuyinCardPay(pay_sn);
                            break;
                        case 1://支付宝支付
                            goAlipay(pay_sn);
                            break;
                        case 2://企业银联
                            goQiyePay(pay_sn);
                            break;
                        case 3://银联
                            goBankPay(pay_sn);
                            break;
                        case 4://融资
                            goFinancePay(pay_sn);
                            break;
                        case 5://银行转账
                            goTransferPay(pay_sn);
                            break;
                    }
                }else{
                    ToastUtils.showToast("请至少选择一种支付方式");
                }
                break;
            case R.id.go_to_rule:
                Intent intentSecond = new Intent(this, FinanceProcessActivity.class);
                intentSecond.putExtra("from","second");
                startActivity(intentSecond);
                break;
        }
    }

    /**
     * 转账
     * @param pay_sn
     */
    private void goTransferPay(String pay_sn) {
        Intent intent = new Intent(this, BankTranslateActivity.class);
        intent.putExtra("pay_sn",pay_sn);
        startActivity(intent);
        finish();
    }

    /**
     * 融资租赁
     * @param pay_sn
     */
    private void goFinancePay(String pay_sn) {
//        ToastUtils.showToast("目前仅支持支付宝支付，敬请期待");
        Intent intent = new Intent(this, AptitudeSubmitActivity.class);
        intent.putExtra("pay_sn",pay_sn);
        startActivity(intent);
    }

    /**
     * 银联
     * @param pay_sn
     */
    private void goBankPay(String pay_sn) {
        ToastUtils.showToast("目前仅支持支付宝支付，敬请期待");
    }

    /**
     * 企业银联
     * @param pay_sn
     */
    private void goQiyePay(String pay_sn) {
        ToastUtils.showToast("目前仅支持支付宝支付，敬请期待");
    }

    /**
     * 都银卡抵扣
     * @param pay_sn
     */
    private void goDuyinCardPay(String pay_sn) {
        Intent intent = new Intent(this, DuYinCardPayActivity.class);
        intent.putExtra("pay_sn",pay_sn);
        startActivity(intent);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constant.REQUEST_SUCCESS:
                    Intent intent = new Intent(AccessoryProtectPayActivity.this, WebActivity.class);
                    intent.putExtra("html", (String) msg.obj);
                    startActivity(intent);
                    finish();
                    break;
                case Constant.REQUEST_FAIL:
                    ToastUtils.showToast((String) msg.obj);
                    Intent intent1 = new Intent(AccessoryProtectPayActivity.this, MainActivity.class);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent1);
                    break;
            }
        }
    };

    /**
     * 支付宝接口
     * @param pay_sn
     */
    private void goAlipay(String pay_sn) {
        StringRequest sr = new StringRequest(Request.Method.GET, Constant.URL_ALIPAY + "&key=" + MyApplication.getmInstance().getKey() + "&pay_sn=" + pay_sn,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("------->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            String error = datas.getString("error");
                            mHandler.obtainMessage(Constant.REQUEST_FAIL, error).sendToTarget();
                        } catch (JSONException e) {
                            mHandler.obtainMessage(Constant.REQUEST_SUCCESS, s).sendToTarget();
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    private void changeBoxState(CheckBox cb_01, CheckBox cb_02, CheckBox cb_03, CheckBox cb_04, CheckBox cb_05, CheckBox cb_06) {
        cb_01.setChecked(true);
        cb_02.setChecked(false);
        cb_03.setChecked(false);
        cb_04.setChecked(false);
        cb_05.setChecked(false);
        cb_06.setChecked(false);
    }

}
